package org.yu.register.recipe;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CustomRecipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.Tags;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import org.yu.register.ItemRegister;
import org.yu.register.RecipeRegister;

import javax.annotation.Nonnull;

public class BagSealedRecipe extends CustomRecipe {
    public BagSealedRecipe(ResourceLocation pId) {
        super(pId);
    }

    public boolean matches(CraftingContainer pInv, @Nonnull Level pLevel) {
        ItemStack container = ItemStack.EMPTY;
        ItemStack string=ItemStack.EMPTY;
        for(int i = 0; i < pInv.getContainerSize(); ++i) {
            ItemStack stackIndex = pInv.getItem(i);
            if (!stackIndex.isEmpty()) {
                if (stackIndex.getItem().equals(ItemRegister.silkBag.asItem())) {
                    if (!container.isEmpty()) {
                        return false;
                    }
                    container = stackIndex;
                } else {
                    if (!stackIndex.is(Tags.Items.STRING)) {
                        if(!string.isEmpty())
                        return false;
                    }
                    string=stackIndex;
                }
            }
        }

        return !container.isEmpty() && !string.isEmpty();
    }

    @Nonnull
    public ItemStack assemble(CraftingContainer pInv) {
        for(int i = 0; i < pInv.getContainerSize(); ++i) {
          var item=pInv.getItem(i);
          if(item.getItem().equals(ItemRegister.silkBag.asItem())){
              ItemStack sealedItem=new ItemStack(ItemRegister.silkBagSealed);
              sealedItem.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(
                      sealedHandle->{
                          var sealedStackHandle=(ItemStackHandler)sealedHandle;
                          item.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
                                  .ifPresent(itemHandle->{
                                      var itemStackHandle=(ItemStackHandler)itemHandle;
                                      sealedStackHandle.deserializeNBT(itemStackHandle.serializeNBT());
                                  });
                      }
              );
              return sealedItem;
          }
        }
        return ItemStack.EMPTY;
    }

    @Nonnull
    @Override
    public ItemStack getResultItem() {
        return new ItemStack(ItemRegister.silkBagSealed);
    }

    @Override
    public boolean canCraftInDimensions(int pWidth, int pHeight) {
        return true;
    }

    @Nonnull
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeRegister.bagSealed;
    }
}
